﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE80;
using EnvDTE;
using System.IO;
using System.Windows.Forms;

namespace iMainAddin
{
    public class iProjectItem
    {
        ProjectItemsEvents mItemRemoved;
        SelectionEvents mSelectEvent;
        DialogResult mDeleteItem;
        DTE2 mApplication;
        int mSelectCount;
        int mDeleteCount;

        public iProjectItem(DTE2 application)
        {
            mApplication = application;
            mDeleteCount = 0;
            mItemRemoved = ((EnvDTE80.Events2)application.Events).ProjectItemsEvents;
            mSelectEvent = ((EnvDTE80.Events2)application.Events).SelectionEvents;
            mSelectEvent.OnChange += new _dispSelectionEvents_OnChangeEventHandler(this.SelectCheck);
            mItemRemoved.ItemRemoved += new _dispProjectItemsEvents_ItemRemovedEventHandler(this.RemoveItemfile);
        }

        protected void SelectCheck()
        {
            mSelectCount = mApplication.SelectedItems.Count;
        }

        protected void RemoveItemfile(ProjectItem projectItem)
        {
            RemoveCheck();

            String fileName = projectItem.get_FileNames(0);
            if (mDeleteItem == DialogResult.Yes)
            {
                String projetPath = projectItem.ContainingProject.FullName;
                if( iSystem.iFileAssist.RemoveTail( ref projetPath, @"Win32\VC90") )
                {
                    String temp = fileName;
                    if (iSystem.iFileAssist.RemoveTail(ref temp, projectItem.Name) 
                        && projetPath.Equals(temp, StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (!iSystem.iFileAssist.DeleteToRecycle(fileName))
                            MessageBox.Show("파일 삭제를 실패 했습니다");  
                    }
                }                
            }
        }

        private void RemoveCheck()
        {
            if (mDeleteCount == 0)
            {
                mDeleteCount = mSelectCount;
                mDeleteItem = MessageBox.Show("파일을 폴더에서도 삭제 하시겠습니까?", "파일 삭제",
                    MessageBoxButtons.YesNo);
            }
            --mDeleteCount;
        }
    }
}
